//Page Object
// 0 引入 发送请求的方法 一定要把路径补全
import { } from "../../request/index.js"
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';
const $http = require('../../request/index');
Page({
  data: {
    score: 0,
    code: "",
    // 轮播图数据
    swiperList: [
      {
        goods_id: 129,
        image_src: "https://i0.hdslb.com/bfs/album/56effedf08a9e774fa2f2cf9181c81e589116aec.png"
      },
      {
        goods_id: 130,
        image_src: "https://i0.hdslb.com/bfs/album/f786efb403bbecbc3eb2ac2db6553b850d210707.png"
      }, {
        goods_id: 131,
        image_src: "https://i0.hdslb.com/bfs/album/78b125b1cc21b36fa3dd7ca3beb50ef500c3834d.png"
      },
    ],
  },
  //  发送异步请求 获取轮播图0数据 优化手段可以通过 es6的 promise 解决
  //options(Object)
  onLoad: function (options) {
    this.user_Login();
  },
  onShow: function () {
    // 在访问user页面的时候进行登录 通过返回的头像数据 判断是否是首次登录 如果是的话 就
    // 显示登录成功 否则跳转到授权 页面进行用户授权 拿到他的头像和名称之后将其  
  },
  user_Login() {
    // 1.登录 存储token
    wx.login({
      success: (res) => {
        let code = res.code;
        // 发送登录请求, 拿到token
        $http.request("GET", `/front/user/login?code=${code}`, "").then(res => {
          let { data } = res.data
          console.log("token", data)
          // 将wx的token 存入到 localstroge中
          wx.setStorageSync("token", data);
          this.get_current_user_message();
        })
      }
    })
  },
  // 获取个人信息 并调用方法 set全局变量
  get_current_user_message() {
    $http.request("GET", `/front/user/current`, "").then(res => {
      let { data } = res.data
      console.log("userMessage", data)
      if (data.name) { //若头像信息不为null => 该用户已经授权过 且信息较为完善, 则对全局变量进行赋值
        this.set_global_current_user_message(data)
      }
      // 否则 不予赋值 (都是null)
    })
  },
  // 设置个人的信息(设置分数信息)
  set_global_current_user_message(data) {
    let app = getApp();
    if (data.gender == "女") {
      app.userInfo.gender = 1;
    } else {
      app.userInfo.gender = 0;
    }
    app.userInfo.classId = data.classId
    app.userInfo.avatarUrl = data.avatarUrl;
    app.userInfo.className = data.className;
    app.userInfo.deptName = data.deptName;
    app.userInfo.id = data.id;
    app.userInfo.name = data.name;
    app.userInfo.number = data.number;
    app.userInfo.phone = data.phone;
    app.userInfo.schoolName = data.schoolName;
    app.userInfo.totalScore = data.totalScore;
    this.setData({
      score: app.userInfo.totalScore
    })
  },
  // 点击第一个图片 跳转进入新生练习
 

});
